
package com.borqs.music.tasks;

import static com.borqs.music.http.RequestMethod.GET;
import static com.borqs.music.util.MusicConf.ReqParam.SINGER;
import static com.borqs.music.util.MusicConf.ReqParam.TITLE;
import static com.borqs.music.util.MusicConf.ReqType.MUSIC_SEARCH_LYRIC;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.borqs.music.executor.Task;
import com.borqs.music.executor.TaskListener;
import com.borqs.music.http.HttpRequest;
import com.borqs.music.http.HttpResponse;
import com.borqs.music.util.MusicConf;

public class ManualSearchLyricTask extends Task {
    private String mTicket;
    private String mTitle;
    private String mSinger;

    public ManualSearchLyricTask(TaskListener listener, String title, String singer) {
        super(listener);
        if (title == null) {
            mTitle = "";
        } else {
            mTitle = title;
        }

        if (singer == null) {
            mSinger = "";
        } else {
            mSinger = singer;
        }
    }

    @Override
    public HttpRequest prepareRequest() {
        String url = null;
        Map<String, String> params = new HashMap<String, String>();
        url = MusicConf.getUrl(MUSIC_SEARCH_LYRIC);
        params.put(TITLE, mTitle);
        params.put(SINGER, mSinger);

        HttpRequest req = new HttpRequest(GET, url, mTicket , params);
        return req;
    }

    @Override
    public Object handleResponse(HttpResponse resp) throws Exception {
        JSONArray jArr = resp.asJSONArray();
        if (jArr != null && jArr.length() > 0) {
            JSONObject jObj = jArr.getJSONObject(0);
            return jObj.getString("lyric");
        } else {
            return null;
        }
    }

    public void setTicket(String ticket) {
        mTicket = ticket;
    }
}
